#include "newcanselinfopage.h"
#include "ui_newcanselinfopage.h"
#include<QListWidget>
#include <QVBoxLayout>
#include<QMessageBox>
//initialization
NewCanSelInfoPage* NewCanSelInfoPage::instance = nullptr;
QString NewCanSelInfoPage::comboxselchannelnum = "1";
NewCanSelInfoPage::NewCanSelInfoPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::NewCanSelInfoPage)
{
    ui->setupUi(this);
   //  //add new save
   //  QListWidget *applicationsListBox = new QListWidget(this);
   //  QStringList applications;
   //  for (int i = 1; i <= 30; ++i)
   //      applications.append(tr("Application %1").arg(i));
   //  applicationsListBox->insertItems(0, applications);

   //  applicationsListBox->setAlternatingRowColors(true);
   //  applicationsListBox->setStyleSheet("QTableView::item:alternate:!selected, QTableWidget::item:alternate:!selected, QListView::item:alternate:!selected { background: rgb(222,144,255); } "
   //                                 "QTableView::item:!alternate:!selected, QTableWidget::item:!alternate:!selected, QListView::item:!alternate:!selected { background: rgb(135,206,255); }");
   //  //set applicationsListBox width height
   //  applicationsListBox->setUniformItemSizes(true);
   //  applicationsListBox->resize(300,200);
   // // applicationsListBox->
   //  QVBoxLayout *layout = new QVBoxLayout;
   //  layout->addWidget(applicationsListBox);
   //  setLayout(layout);
}

NewCanSelInfoPage::~NewCanSelInfoPage()
{
    delete ui;
}
//make it mutex
//C:\Users\Administrator\Desktop\kvaser\MYPRO\MyVector\newcanselinfopage.cpp:-1: error: undefined reference to `NewCanSelInfoPage::comboxselchannelnum'
QString NewCanSelInfoPage::getcomboxselchannel()
{
    return comboxselchannelnum;
}

void NewCanSelInfoPage::setcomboxselchannel(QString &numchannel)
{
  comboxselchannelnum =numchannel;
}
NewCanSelInfoPage* NewCanSelInfoPage::getInstance()
{
    //multi thread to access it ,may cause compete.
    static QMutex mutex;
    if(!instance)
    {
        //make it to lock to solve thread saftey
        QMutexLocker locker(&mutex);
        if(!instance)
        {
            qDebug()<<"new NewCanSelInfoPage";
            instance = new NewCanSelInfoPage;
        }
    }
    return instance;
}

// ApplicationsTab::ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent)
//     : QWidget(parent)
// {
//     QLabel *topLabel = new QLabel(tr("Open with:"));

//     QListWidget *applicationsListBox = new QListWidget;
//     QStringList applications;

//     for (int i = 1; i <= 30; ++i)
//         applications.append(tr("Application %1").arg(i));
//     applicationsListBox->insertItems(0, applications);

//     QCheckBox *alwaysCheckBox;

//     if (fileInfo.suffix().isEmpty())
//         alwaysCheckBox = new QCheckBox(tr("Always use this application to "
//                                           "open this type of file"));
//     else
//         alwaysCheckBox = new QCheckBox(tr("Always use this application to "
//                                           "open files with the extension '%1'").arg(fileInfo.suffix()));

//     QVBoxLayout *layout = new QVBoxLayout;
//     layout->addWidget(topLabel);
//     layout->addWidget(applicationsListBox);
//     layout->addWidget(alwaysCheckBox);
//     setLayout(layout);
// }

void NewCanSelInfoPage::on_pushButtonCan_clicked()
{
    //add an dock widget to mainwindow

    QMessageBox msgBox;
    msgBox.setText("Tips");
    msgBox.setInformativeText("Do You confirmed to create channels?");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        //get instance
        //setting channel number
        QString channelnum = ui->comboBoxChannel->currentText();
        qDebug()<<"this time settings channensel = "<<channelnum;
        qDebug()<<"get former :"<<NewCanSelInfoPage::getcomboxselchannel();
        //set  channelnum  to comboxselchannelnum
        NewCanSelInfoPage::setcomboxselchannel(channelnum);
        qDebug()<<"NewCanSelInfoPage settings :"<<NewCanSelInfoPage::getcomboxselchannel();
        CanSettings *instance = CanSettings::getInstance();
        instance->updatetreeview(channelnum);
        instance->show();
    }
    else
    {
        QMessageBox::information(nullptr, "cancel", "You have canceled your choice.");
    }


}

